package group2assignone.ucm.customer;

import group2assignone.dao.MyDAO;
import group2assignone.model.Booking;
import group2assignone.model.Customer;
import group2assignone.model.Flight;
import group2assignone.ucm.UCController;
import group2assignone.ucm.UCStartup;
import group2assignone.view.customer.BookFlightView;
import group2assignone.view.customer.BookingConfirmedView;
import javax.swing.JOptionPane;

/**
 *Use Case Controller - allows the user to book a flight.
 * (Logged in or not)
 */
public class UCBookFlight implements UCController {

    private Flight flight;
    private Booking booking;
    private Customer customer;
    private BookFlightView view;
    private boolean authok,  ok;
    private int option;

    /**
     * assigns customer, flight and view
     * @param customer
     * @param flight
     * @param view
     */
    public UCBookFlight(Customer customer, Flight flight, BookFlightView view) {
        this.customer = customer;
        this.flight = flight;
        this.view = view;
    }

    /**
     * Use Case: Book Flight
     * Pre-Condition: Flight UI
     * Post-Condition: UI is updated
     * 1. User selects a Flight
     * 2. User selects 'book this flight' button
     * 3. System updates UI for more booking options
     * 4. User inputs booking details and clicks ok
     * 5. System verifies details
     * 6. System updates flight details, user details and UI
     */
    public void run() {
        authok = true;//check flight ticket availabilities
        booking = new Booking("0001");
        booking.addFlight(flight.getFlightNum(), customer.getUsername());
       
        /*   ok = flight.update(View.getFlightDetails);   */
        if (authok) {
            option = view.confirmBook();
            if (option == JOptionPane.YES_OPTION) {
                ok = true;//delete account from database (call function from DAO, return ok)
                if (ok) {
                    booking.makeBooking();
                    BookingConfirmedView gui = new BookingConfirmedView(customer, booking);
                    gui.go();
                    view.dispose();
                /*   for(i = 0; i<View.reserved; i++){
                customer.addFlight(flight); */
                } else {
                    //error message (flight not available)
                }
            //error message
            }
        }
    }
}
